package org.example3;

public class MainTest {
    public static void main(String[] args) {

        MyObject myObject = new MyObject();

        //不能从静态方法中引用this
//        myObject.show(()->{
//            System.out.println(this.getClass().getName() + "------" + this);
//            boolean isFunction = this instanceof Function;
//            System.out.println("是否为Function的子类:" + isFunction);
//        });

        myObject.show(new Function() {
            @Override
            public void doSomething() {
                System.out.println(this.getClass().getName() + "----匿名内部类方式:------" + this);
                boolean isFunction = this instanceof Function;
                System.out.println("是否为Function的子类:" + isFunction);
            }
        });

        new MainTest().function();
    }

    public void function() {
        MyObject myObject = new MyObject();
        myObject.show(() -> { //注意这里是myObject实例调用的,this的类型却是org.example3.MainTest
            System.out.println(this.getClass().getName() + "---特别注意---" + this);
            boolean isFunction = this instanceof Function;
            System.out.println("是否为Function的子类:" + isFunction);
        });
    }

}

interface Function {
    void doSomething();
}

class MyObject {
    public void show(Function function) {
        function.doSomething();
    }
}